# -*- coding: utf-8 -*-
# 1、用F12获取可以下载图片的url；
# 2、用parse.unquote()进行解码，然后将200换成0，即真实的高清壁纸；
# 3、url中的parge参数修改其值，来实现翻页，区间范围1-20页，区间0-19


from urllib import parse , request
import requests ,threading
import os

headers = {
   'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
   'referer': 'https://pvp.qq.com/web201605/wallpaper.shtml'
    }

def extract_image(data):
    image_urls = []
    for x in range(1,9):
        img_url =parse.unquote(data['sProdImgNo_%d'%x]).replace('200', '0')
        image_urls.append(img_url)
    return image_urls
def main():
    page_url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page=0&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1620637560232'
    resp = requests.get(page_url,headers)
    result = resp.json()
    datas = result['List']
    for data in datas:
        image_urls = extract_image(data)
        name = parse.unquote(data['sProdName']).replace('1:1', '').strip()
        # images_1\猪八戒-年年有余
        dirpath = os.path.join('images_1',name)
        os.mkdir(dirpath)
        for index,img_url in enumerate(image_urls):
            request.urlretrieve(img_url,os.path.join(dirpath,'%d.jpg'%(index+1)))
            print(f'{img_url}已下载完成')
            
    
if __name__ == '__main__':
    main()
    